import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '../widgets/AppColors.dart';

class weekTime extends StatefulWidget {
  final Function(DateTime) onDateChanged;

  weekTime({
    Key? key,
    required this.onDateChanged,
  }) : super(key: key);

  @override
  _weekTimeState createState() => _weekTimeState();
}

class _weekTimeState extends State<weekTime> {
  late DateTime _startDate;
  late DateTime _endDate;

  @override
  void initState() {
    super.initState();
    _startDate =
        DateTime.now().subtract(Duration(days: DateTime.now().weekday - 1));
    _endDate = _startDate.add(Duration(days: 6));
  }

  void _changeDate(int value) {
    setState(() {
      DateTime newStartDate = _startDate.add(Duration(days: value * 7));
      if (newStartDate.isAfter(
          DateTime.now().add(Duration(days: 6 - DateTime.now().weekday)))) {
        return;
      }
      _startDate = newStartDate;
      _endDate = _startDate.add(Duration(days: 6));
      widget.onDateChanged(_startDate);
    });
  }

  String _formatDate(DateTime date) {
    return '${date.year}.${date.month.toString().padLeft(2, '0')}.${date.day.toString().padLeft(2, '0')}';
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        IconButton(
          icon: Image.asset('assets/images/centre/icon_left.png'),
          onPressed: () => _changeDate(-1),
        ),
        Text(
          '${_formatDate(_startDate)} - ${_formatDate(_endDate)}',
          style: TextStyle(color: const Color(0xFFFCAA3F), fontSize: 16),
        ),
        IconButton(
          icon: Image.asset('assets/images/centre/icon_right.png'),
          onPressed: () => _changeDate(1),
        ),
      ],
    );
  }
}
